package com.chhuang.system.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.validation.ValidGroupSave;
import com.chhuang.system.bean.ChRoleQueryBean;
import com.chhuang.system.consts.SysConstants;
import com.chhuang.system.po.ChRole;
import com.chhuang.system.po.ChRoleMenuMap;
import com.chhuang.system.po.ChUserRoleMap;
import com.chhuang.system.service.IChRoleMenuMapService;
import com.chhuang.system.service.IChRoleService;
import com.chhuang.system.service.IChUserRoleMapService;
import com.chhuang.utils.Assert;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/system/role")
public class ChRoleController {
	private static final Log log = LogFactory.getLog(ChRoleController.class);
	
	@Autowired @Qualifier("chRoleService")
	private IChRoleService chRoleService;
	@Autowired @Qualifier("chUserRoleMapService")
	private IChUserRoleMapService chUserRoleMapService;
	@Autowired @Qualifier("chRoleMenuMapService")
	private IChRoleMenuMapService chRoleMenuMapService;
	
	/**
	 * 根据角色id获取角色信息
	 * @param roleId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/findRoleById",method=RequestMethod.POST)
	@ResponseBody
	public Object findRoleById(String roleId) throws Exception{
		ChRole role = null;
		if(Assert.isValidString(roleId)){
			role = chRoleService.findByPrimaryKey(roleId);
		}
		log.debug("根据角色id获取角色信息");
		return role;
	}

	/**
	 * 获取所有角色信息
	 * @param roleId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/findRoles",method=RequestMethod.GET)
	@ResponseBody
	public Object findRoles() throws Exception{
		List<ChRole> roles = chRoleService.findAllRoles();
		log.debug("获取所有角色信息");
		return roles;
	}
	
	/**
	 * 获取角色列表
	 * @param roleQueryBean
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/getList",method=RequestMethod.POST)
	@ResponseBody
	public Object getList(@RequestBody ChRoleQueryBean roleQueryBean) throws Exception{
		log.debug(roleQueryBean.getPageSize()+";"+roleQueryBean.getPageNum()
				+";"+roleQueryBean.getOrderBy()+";"+roleQueryBean.isDesc()
				+";"+roleQueryBean.isOr());
		
		QueryBean<ChRole> queryBean = chRoleService.seniorQuery(roleQueryBean);
		
		return queryBean;
	}
	
	/**
	 * 添加或修改角色信息
	 * @param role
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/editRole",method=RequestMethod.POST)
	@ResponseBody
	public Object editRole(@RequestBody @Validated(value={ValidGroupSave.class}) ChRole role, BindingResult bindingResult) throws Exception{
		JSONObject json = new JSONObject();
		if(bindingResult.hasErrors()){
			json.put(SysConstants.Status.SUCCESS, false);
			json.put(SysConstants.Status.RESULT, bindingResult.getAllErrors().get(0).getDefaultMessage());
			log.debug(json.toString());
			return json;
		}
		if(chRoleService.saveOrUpdate(role)>0){
			List<ChUserRoleMap> userRoleMaps=chUserRoleMapService.findByRoleId(role.getRoleId());
			boolean equelsOrder = false;
			if(Assert.isValidCollection(userRoleMaps)){
				if(userRoleMaps.get(0).getRoleOrder()==role.getRoleOrder()){
					equelsOrder = true;
				}
				if(!equelsOrder){
					for(ChUserRoleMap userRoleMap : userRoleMaps){
						userRoleMap.setRoleOrder(role.getRoleOrder());
						chUserRoleMapService.update(userRoleMap);
					}
				}
			}
			if(!equelsOrder){
				List<ChRoleMenuMap> roleMenuMaps=chRoleMenuMapService.findByRoleId(role.getRoleId());
				if(Assert.isValidCollection(roleMenuMaps)){
					for(ChRoleMenuMap roleMenuMap : roleMenuMaps){
						roleMenuMap.setRoleOrder(role.getRoleOrder());
						chRoleMenuMapService.update(roleMenuMap);
					}
				}
			}
			json.put(SysConstants.ROLE_ID, role.getRoleId());
			json.put(SysConstants.Status.SUCCESS, true);
		}else{
			json.put(SysConstants.Status.SUCCESS, false);
			json.put(SysConstants.Status.RESULT, "未知错误");
		}
		log.debug(json.toString());
		return json;
	}
	
	/**
	 * 删除角色信息
	 * @param ids
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/delRoles/{ids}",method=RequestMethod.DELETE)
	@ResponseBody
	public Object delRoles(@PathVariable String ids) throws Exception{
		JSONObject json = new JSONObject();
		if(Assert.isValidString(ids)){
			String[] roleIds = ids.split(SysConstants.Symbol.COMMA);
			chRoleService.deleteByRoleIdArray(roleIds);
			chUserRoleMapService.deleteByRoleIdArray(roleIds);
			chRoleMenuMapService.deleteByRoleIdArray(roleIds);
			json.put(SysConstants.Status.SUCCESS, true);
		}
		log.debug(ids+":::"+json.toString());
		return json;
	}
}
